package com.education.util;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import javax.mail.Address;
import javax.mail.SendFailedException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

/**
 * Created by lzh on 2017/8/20.
 */
public class SendMailUtil {

    private static  String hostName;
    private static  int smtpPort;
    private static  String from;
    private static  String charset;
    private static  boolean SSLOnConnect;
    private static  boolean debug;
    private static  String username;
    private static  String password;
    private static boolean isReSend;
    
    static {
        Properties prop = new Properties();
        InputStream in = SendMailUtil.class.getResourceAsStream("/mailconfig.properties");
        try {
            prop.load(in);
            hostName=prop.getProperty("email.hostName");
            smtpPort=Integer.parseInt(prop.getProperty("email.smtpPort"));
            from=prop.getProperty("email.from");
            charset=prop.getProperty("email.charset");
            SSLOnConnect=Boolean.parseBoolean(prop.getProperty("email.SSLOnConnect"));
            debug=Boolean.parseBoolean(prop.getProperty("email.debug"));
            username=prop.getProperty("email.username");
            password=prop.getProperty("email.password");
            isReSend=Boolean.parseBoolean(prop.getProperty("email.isReSend"));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static int send(EmailMsgModel emailMsgModel){

        if (emailMsgModel!=null){
            SimpleEmail simpleEmail=new SimpleEmail();

            try {
                simpleEmail.setHostName(hostName);
                simpleEmail.setAuthentication(username,password);
                simpleEmail.setCharset(charset);
                simpleEmail.setFrom(from);
                simpleEmail.setDebug(debug);
                simpleEmail.setSentDate(new Date());
                simpleEmail.setSSLOnConnect(SSLOnConnect);
                simpleEmail.setSmtpPort(smtpPort);
                simpleEmail.setMsg(emailMsgModel.getMsg());
                simpleEmail.setSubject(emailMsgModel.getSubject());
                simpleEmail.addTo(emailMsgModel.getAddTo());
                simpleEmail.send();
                return emailMsgModel.getAddTo().length;
            }
            catch (EmailException e) {//getCause得到方法封装之前的异常
                if (isReSend){
                    if (e.getCause() instanceof SendFailedException){

                        SendFailedException sendFailedException=(SendFailedException)e.getCause();
                        Address[] validAddresses = sendFailedException.getValidUnsentAddresses();//获取所有正常接收的地址
                        if (validAddresses!=null){
                            String[] strings=new String[validAddresses.length];
                            for(int i=0;i<strings.length;i++){
                                strings[i]=validAddresses[i].toString();
                            }
                            try {
                                simpleEmail.addTo(strings);
                                simpleEmail.send();
                                return strings.length;
                            }
                            catch (EmailException e1) {
                                e1.printStackTrace();
                            }
                    }

                    }
                }
                e.printStackTrace();
            }
        }
        return -1;
    }
}
